"""
管理后台 - 招聘职位管理接口
"""
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession

from app.dependencies import get_db, get_current_user
from app.schemas.common import ResponseModel, PaginatedResponse
from app.schemas.content import JobCreate, JobUpdate, JobResponse
from app.crud.base import CRUDBase
from app.models.content import Job


router = APIRouter()
crud_job = CRUDBase[Job, JobCreate, JobUpdate](Job)


@router.get("", response_model=ResponseModel)
async def get_jobs(
    page: int = Query(1, ge=1),
    page_size: int = Query(10, ge=1, le=100),
    location: Optional[str] = Query(None),
    status: Optional[int] = Query(None),
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取职位列表
    """
    filters = {}
    if location:
        filters["location"] = location
    if status is not None:
        filters["status"] = status
    
    skip = (page - 1) * page_size
    jobs = await crud_job.get_multi(db, skip=skip, limit=page_size, filters=filters)
    total = await crud_job.get_count(db, filters=filters)
    
    return ResponseModel(
        data=PaginatedResponse(
            total=total,
            page=page,
            page_size=page_size,
            items=[JobResponse.model_validate(j) for j in jobs]
        )
    )


@router.get("/{job_id}", response_model=ResponseModel)
async def get_job(
    job_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取职位详情
    """
    job = await crud_job.get_by_id(db, id=job_id)
    if not job:
        raise HTTPException(status_code=404, detail="职位不存在")
    
    return ResponseModel(data=JobResponse.model_validate(job))


@router.post("", response_model=ResponseModel)
async def create_job(
    job_in: JobCreate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    创建职位
    """
    job = await crud_job.create(db, obj_in=job_in)
    return ResponseModel(
        code=201,
        message="创建成功",
        data=JobResponse.model_validate(job)
    )


@router.put("/{job_id}", response_model=ResponseModel)
async def update_job(
    job_id: int,
    job_in: JobUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    更新职位
    """
    job = await crud_job.get_by_id(db, id=job_id)
    if not job:
        raise HTTPException(status_code=404, detail="职位不存在")
    
    job = await crud_job.update(db, db_obj=job, obj_in=job_in)
    return ResponseModel(
        message="更新成功",
        data=JobResponse.model_validate(job)
    )


@router.delete("/{job_id}", response_model=ResponseModel)
async def delete_job(
    job_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    删除职位
    """
    success = await crud_job.delete(db, id=job_id)
    if not success:
        raise HTTPException(status_code=404, detail="职位不存在")
    
    return ResponseModel(message="删除成功")

